package com.luohoufa.ms.user.controller;

import com.luohoufa.ms.user.constant.CookieConstant;
import com.luohoufa.ms.user.constant.RedisConstant;
import com.luohoufa.ms.user.enums.ResultEnum;
import com.luohoufa.ms.user.enums.RoleEnum;
import com.luohoufa.ms.user.model.UserInfo;
import com.luohoufa.ms.user.service.UserService;
import com.luohoufa.ms.user.utils.CookieUtil;
import com.luohoufa.ms.user.utils.ResultVOUtil;
import com.luohoufa.ms.user.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 买家登录
     * @param openid
     * @param httpServletResponse
     * @return
     */
    @GetMapping("/buyer")
    public ResultVO buyer(@RequestParam("openid") String openid,
                          HttpServletResponse httpServletResponse) {
        /**
         * 1. openid和数据库中的openid比较
         * 2. 判断角色
         * 3. cookie里设置openid
          */
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null) {
            return ResultVOUtil.build(ResultEnum.LOGIN_FAIL);
        }

        if (!RoleEnum.BUYER.getCode().equals(userInfo.getRole())) {
            return ResultVOUtil.build(ResultEnum.ROLE_ERROR);
        }

        CookieUtil.set(httpServletResponse, CookieConstant.OPENID, openid, CookieConstant.EXPIRE);

        return ResultVOUtil.success();

    }

    /**
     * 卖家登录
     * @param openid
     * @param httpServletResponse
     * @return
     */
    @GetMapping("/seller")
    public ResultVO seller(@RequestParam("openid") String openid,
                          HttpServletRequest httpServletRequest,
                          HttpServletResponse httpServletResponse) {
        /**
         * 1. openid和数据库中的openid比较
         * 2. 判断角色
         * 3. redis设置key=UUID, value=openid
         * 4. cookie里设置token=UUID
         */

        // 取cookie
        Cookie cookie = CookieUtil.get(httpServletRequest, CookieConstant.TOKEN);
        if (cookie != null && StringUtils.isNotEmpty(stringRedisTemplate.opsForValue().get(cookie.getValue()))) {
            return ResultVOUtil.success();
        }

        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null) {
            return ResultVOUtil.build(ResultEnum.LOGIN_FAIL);
        }

        if (!RoleEnum.SELLER.getCode().equals(userInfo.getRole())) {
            return ResultVOUtil.build(ResultEnum.ROLE_ERROR);
        }

        String redisKey = String.format(RedisConstant.TOKEN_TEMPLATE, UUID.randomUUID().toString());
        Integer expire = CookieConstant.EXPIRE;
        stringRedisTemplate.opsForValue().set(redisKey, openid, expire, TimeUnit.SECONDS);

        CookieUtil.set(httpServletResponse, CookieConstant.TOKEN, redisKey, CookieConstant.EXPIRE);

        return ResultVOUtil.success();
    }

}
